package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.ISignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SignServiceImpl implements ISignService {

    @Autowired
    SignMapper signMapper;

    //添加一条签到记录
    public Sign save(Sign record) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //当天 newDate
        String newDate = sdf.format(date);
        //昨天 lastDate
        Calendar yesterday = Calendar.getInstance();
        yesterday.setTime(date);
        yesterday.add(Calendar.DATE, -1);// 日期减1
        Date endDate = yesterday.getTime();
        String lastDate = sdf.format(endDate);
        //查询当天是否有签到记录
        Sign signType = signMapper.selectByUserId(record.getUser().getId(),newDate);
        //如果当天还没签到
        if ( signType == null) {
            //设置签到时间
            record.setSignTime(date);

            //判断昨天是否签到,是的话积分+2,count+1,state=1, 否的话积分+1,count=1,state=0
            int SignCount = signMapper.selectBySignTime(record.getUser().getId(),lastDate);
            if (SignCount==1){
                //获取当前积分: 根据userId 先把当前record 的最近一次签到的积分查出来
                int signScore =signMapper.selectTopScore(record.getUser().getId());
                //设置积分+2
                record.setSignScore(signScore+ 2);
                record.setSignState(2);
                //续签天数+1 : 把当前最新签到信息的count续签天数查出来
                int count =signMapper.selectCount(record.getUser().getId());
                 record.setCount(count +1);
                 //如果续签天数是3的倍数,积分+10
                if ((count+1) % 3 ==0){
                    record.setSignScore(signScore+2+10);
                    record.setSignState(12);
                }

            }else {//非续签
                List<Sign> signState = signMapper.selectSignState(record.getUser().getId());
                //判断是否是首签 : 根据用户id查询
                if (signState.size()==0) { //即首次签到,直接设置积分=1
                    record.setSignScore(1);
                    record.setSignState(1);
                }else {
                    //获取当前积分: 根据userId 先把当前record 的最近一次签到的积分查出来
                    int signScore = signMapper.selectTopScore(record.getUser().getId());
                     //设置积分+1
                    record.setSignScore(signScore+ 1);
                    record.setSignState(1);
                }
                    //设置续签天数=1
                    record.setCount(1);
            }
            //添加一条签到记录
            signMapper.insert(record);

        }else { //已经签到了,并把签到的那条信息查出来
            record=signMapper.selectSignId(record.getUser().getId(),newDate);

            throw new RuntimeException("你今天已经签到了哦");
        }
        return record;
    }


    //查询一个用户最新签到信息
    public Sign selectOne(Sign record) {
        record = signMapper.selectByPrimaryKey(record.getUser().getId());
        return record;

    }

    //扣除我的积分 , 添加一条中间表数据
    public void setMySign(int productScore,Long productId, Long UserId) {
        Sign sign = signMapper.selectByPrimaryKey(UserId);
        int signScore = sign.getSignScore();
        int mySign = signScore - productScore;
        //修改后再设回去
        sign.setSignScore(mySign);
        signMapper.updateByPrimaryKey(sign);

        //添加一条中间表
        Long user_id = UserId ;
        Long product_id = productId;
        Date creatTime = new Date();
        signMapper.insertMiddle(user_id,product_id,creatTime);
    }

    //查询商品兑换时间
    public Date selectChangeTime() {
        return signMapper.selectChangeTime();
    }

    //根据用户id去查用户签到总天数
    public int selectCount(Long userId) {
        int allCount = signMapper.selectAllCount(userId);
        System.out.println(allCount);
        return allCount;
    }

    //查询昨天的续签天数
    public int yesterdayCount(Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //昨天 lastDate
        Calendar yesterday = Calendar.getInstance();
        yesterday.setTime(date);
        yesterday.add(Calendar.DATE, -1);// 日期减1
        Date endDate = yesterday.getTime();
        String lastDate = sdf.format(endDate);
        int yesterdayCount = signMapper.yesterdayCount(userId,lastDate);
        return yesterdayCount;
    }

    //判断昨天有没有签到
    public int selectBySignTime(Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //昨天 lastDate
        Calendar yesterday = Calendar.getInstance();
        yesterday.setTime(date);
        yesterday.add(Calendar.DATE, -1);// 日期减1
        Date endDate = yesterday.getTime();
        String lastDate = sdf.format(endDate);
        int SignCount = signMapper.selectBySignTime(userId, lastDate);
        return SignCount;
    }

    //判断用户今天有没有签到
    public Sign getSignByToday(Sign record) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //当天 newDate
        String newDate = sdf.format(date);
        record = signMapper.selectByUserId(record.getUser().getId(),newDate);
        return record;
    }


}